package com.fuyao.cloud.asset.inventory.process;

import cn.hutool.core.lang.ClassScanner;
import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
import springfox.documentation.schema.Maps;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

@Component
@Slf4j
public class HandlerInventoryTaskTypeProcessor implements BeanFactoryPostProcessor {

    private static final String HANDLER_PACKAGE = "com.fuyao.cloud.asset.inventory.process";

    /**
     * 扫描@HandlerType,初始化HandlerContext,将其注册到spring容器
     *
     * @param beanFactory bean工厂
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        log.info("HandlerType init beanFactor");
        Map<String, Class> handlerMap = MapUtil.newHashMap(4);
        ClassScanner.scanAllPackageByAnnotation(HANDLER_PACKAGE, InventoryTaskType.class).forEach(clazz -> {
            String type = clazz.getAnnotation(InventoryTaskType.class).value();
            handlerMap.put(type, clazz);
        });
        // 初始化HandlerContext，将其注册到spring容器
        HandlerInventoryTaskTypeContext context = new HandlerInventoryTaskTypeContext(handlerMap);
        beanFactory.registerSingleton(HandlerInventoryTaskTypeContext.class.getName(), context);
    }
}
